<?

class ViewTwitter	{

	/**
	 * Define Twitter default url 
	 *
	 * @name 	url		Twitter url 
	 */
	const url		= "https://twitter.com/";

	/**
	 * Define Twitter Developers url 
	 *
	 * @name 	devurl	Twitter developers url 
	 */
	const devurl	= "https://dev.twitter.com/";


	function __construct()	{
		
#		Communicate with external twitter class (cheers Tijs Verkoyen)
#		check out documentation on http://classes.verkoyen.eu/twitter_oauth/docs
		require_once ROOT . 'engine/ext/twitter/twitter.php';
		
		$this->account = engine_get( "json:twitter" );

		

	}
	
	function login()	{
		
		if( !$this->account )
			return null;
		
#		setup connection with callback url
		$twitter = new Twitter( $this->account->api, $this->account->secret );
		
#		Authorize
		$twitter->setOAuthToken( $this->account->oauth_token );
		$twitter->setOAuthTokenSecret( $this->account->oauth_token_secret );
		

		
		/*$twitter = new Twitter( $settings->api, $settings->secret );
		
		// authorize
		$twitter->setOAuthToken( $settings->oauth_token );
		$twitter->setOAuthTokenSecret( $settings->oauth_token_secret );*/
		
		
		return $twitter;
	}

	function dashboard()	{
	
		
		if( !$Twitter = $this->login() )
			 return $this->noAccountContent();
	
		$content = '<h1>Twitter</h1>';
		$posts = array();
		
	#	Mentions of account
		if( $tweets = $Twitter->statusesMentions( null, null, 5 ))
			foreach( $tweets as $tweet ) $posts[ $tweet['id']] =  array( 'text'=> $tweet['text'], 'user'=> $tweet['user']['name'] );
	
	#	Retweets of account		
		if( $tweets = $Twitter->statusesReweetsOfMe( null, null, 5 ))
			foreach( $tweets as $tweet ) $posts[ $tweet['id']] =  array( 'text'=> $tweet['text'], 'user'=> $tweet['user']['name'] );

	#	Tweets of account		
		if( $tweets = $Twitter->statusesUserTimeline())// null, null, null, null, null, 12 - count( $posts )))
			foreach( $tweets as $tweet ) $posts[ $tweet['id']] =  array( 'text'=> $tweet['text'] );
		
		krsort( $posts );
		
		return array(	'content'=> $content,
						'elements'=> array(	UList::unsorted( array_slice( $posts, 0, 20 ), 'ulist', 10 ),
											Input::wordcount( 'New Tweet', 'twitter', array( 'key'=> 'post', 'count'=> 140 ))
		));
	
	}
	
	function wordcount( $params )	{
	
		
		if( !$Twitter = $this->login() )
			 return $this->noAccountContent();
	
		$Twitter->statusesUpdate( $params->value );

		
		return array( 'message'=> "Tweet posted!" );
	}
	
	function noAccountContent()	{
		
		return array(	'content'	=> "<h1>Twitter</h1>Your Twitter account is not connected, yet.",
			 			'elements'	=> array( Button::view( 'Connect Twitter', "twitter", "setupAccount" )));
	}
	
	function setupAccount()	{

		return array(	'content'=> "<h1>Connect to Twitter</h1>First we need a special \"API key\". This is the authorisation you give to the Engine to communicate as your account.",
						'elements'	=> array(	Button::link( 'Go to Twitter Dev', self::devurl . 'apps/new' ),
												Text::description( "Log in, then enter <strong>Engine " . engine_get('account') . "</strong> for name, <strong>Connection to my CMS</strong> as description and <strong>" . engine_get('url') . "</strong> as Website. Create and go to the settings tab, select <strong>Read & Write</strong> as Application Type.<br>Once created, copy the appropiate keys and tokens (create access token):" ),
												Input::update( 'Twitter name', 'settings', array( 'key'=> 'twitter.name' )),
												Input::update( 'Consumer key', 'settings', array( 'key'=> 'twitter.api' )),
												Input::update( 'Consumer secret', 'settings', array( 'key'=> 'twitter.secret' )),
												Input::update( 'Access token', 'settings', array( 'key'=> 'twitter.oauth_token' )),
												Input::update( 'Access token secret', 'settings', array( 'key'=> 'twitter.oauth_token_secret' )),
												Button::view( 'Finish', "twitter", "finishSetupAccount", "black" )
	
				));
	
	}
	
	function finishSetupAccount()	{
	
		return $this->dashboard();	
	}

}

?>